/**
 * 详情对话框
 */
var ZxChengbenInfoDlg = {
    data: {
        btyZxMdzxId: "",
        btyyBxdaId: "",
        btyyBxdaIdView: "",
        gysDianhua: "",
        btyZxHuopinId: "",
        btyZxHuopinIdView: "",
        danwei: "",
        chicun: "",
        shuliang: "",
        jiage: "",
        zongjia: "",
        status: "",
        statusView: "",
        delFlag: "",
        createUser: "",
        createUserName: "",
        createTime: "",
        updateUser: "",
        updateUserName: "",
        updateTime: "",
        company: "",
        companyView: "",
        srcType: "",
        srcValue: "",
        riqi: "",
        zhaiyao: "",
        yhKahao: "",
        yhKaihuhang: "",
        daKuan:"",
        beizhu:""
    }
};

layui.use(['form', 'admin', 'ax','laydate'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;

    //让当前iframe弹层高度适应
    admin.iframeAuto();
    form.verify({
        double: function (value) {
            var patt2=/^(\-|\+)?\d+(\.\d+)?$/;
            if (patt2.test(value) == false) {
                return '你输入的不是数字';
            }
        }
    });
    /**
     * 初始化厂家
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyBxda/getAllVendorList");
    var languageResult = languageAjax.start();
    console.info(languageResult);
    $.each(languageResult.data, function (index, val) {
        $("#btyyBxdaId").append("<option value='" + val.yhk + "'>" + val.xm + "</option>");
    });
    form.render('select');

    /**
     * 初始门店
     */
    var languageAjax = new $ax(Feng.ctxPath + "/mdzx/mdzxList");
    var languageResult = languageAjax.start();
    console.info(languageResult);
    $.each(languageResult.data, function (index, val) {
        $("#btyZxMdzxId").append("<option value='" + val.btyZxMdzxId + "'>" + val.btyBrandIdView + "</option>");
    });
    form.render('select');

    //初始货品
    var languageAjax = new $ax(Feng.ctxPath + "/zxHuopin/listHuopinSelect");
    var languageResult = languageAjax.start();
    console.info(languageResult);
    $.each(languageResult.data, function (index, val) {
        $("#btyZxHuopinId").append("<option value='" + val.btyZxHuopinId + "'>" + val.name + "-" +val.zhujima+ "</option>");
    });
    form.render('select');

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/dgdzxBx/detail?btyZxChengbenId=" + Feng.getUrlParam("btyZxChengbenId"));
    var result = ajax.start();
    result.data.riqi=result.data.riqi.substring(0,10);
    form.val('zxChengbenForm', result.data);

    function closePage(){
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        //var btyZxMdzxId = $("#btyZxMdzxId").val();
        // if (btyZxMdzxId == "") {
        //     Feng.error("请选择门店!");
        //     return false;
        // }
        var btyyBxdaId = $("#btyyBxdaId").val();
        var btyyBxdaIdView = $("#btyyBxdaIdView").val();
        if (btyyBxdaId == ""&&btyyBxdaIdView=="") {
            Feng.error("请选择厂家!");
            return false;
        }
        var btyZxHuopinId = $("#btyZxHuopinId").val();
        if (btyZxHuopinId == "") {
            Feng.error("请输入货品!");
            return false;
        }
        var danwei = $("#danwei").val();
        if (danwei == "") {
            Feng.error("请输入货品单位!");
            return false;
        }
        var chicun = $("#chicun").val();
        if (chicun == "") {
            Feng.error("请输入尺寸!");
            return false;
        }
        var shuliang = $("#shuliang").val();
        if (shuliang == "") {
            Feng.error("请输入数量!");
            return false;
        }

        var jiage = $("#jiage").val();
        if (jiage == "") {
            Feng.error("请输入单价!");
            return false;
        }

        var zongjia = $("#zongjia").val();
        if (zongjia == "") {
            // Feng.error("请输入总价!");
            var total=shuliang*jiage;
            $("#zongjia").val(total.toFixed(2));
            // return;
        }

        var ajax = new $ax(Feng.ctxPath + "/dgdzxBx/editItem", function (data) {
            Feng.success("更新成功！");
            //window.location.href = Feng.ctxPath + '/zxChengben?btyZxMdzxId='+Feng.getUrlParam("btyZxMdzxId");
            closePage();
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
        ajax.set("btyyBxdaIdView", $("#btyyBxdaId").find("option:selected").text());
        ajax.set("btyZxHuopinIdView", $("#btyZxHuopinId").find("option:selected").text());
        ajax.set(data.field);
        ajax.start();

        return false;
    });


    //返回按钮
    $("#backupPage").click(function () {
        closePage();
        // window.location.href = Feng.ctxPath + '/zxChengben?btyZxMdzxId='+Feng.getUrlParam("btyZxMdzxId");
    });



    /*form.on('select(queryhp)',function (data) {
        $("#btyZxHuopinId").html("");
        $("#btyZxHuopinId").append("<option value=''>全部</option>");
        var hpajax = new $ax(Feng.ctxPath + "/zxHuopin/listHuopinSelect");
        hpajax.set("mdzxid",data.value);
        var hpajaxResult = hpajax.start();
        $.each(hpajaxResult.data, function (index, val) {
            $("#btyZxHuopinId").append("<option value='" + val.id + "'>" + val.name + "</option>");
        });
        form.render('select');
    });*/



    $("#shuliang").blur(function (data) {
        var shuliang = $("#shuliang").val();
        var jiage = $("#jiage").val();
        var total=shuliang*jiage;
        $("#zongjia").val(total.toFixed(2));
    });
    $("#jiage").blur(function (data) {
        var shuliang = $("#shuliang").val();
        var jiage = $("#jiage").val();
        var total=shuliang*jiage;
        $("#zongjia").val(total.toFixed(2));
    });


    laydate.render({
        elem:'#riqi',
        max:Feng.currentDate()
    });

    //选择厂家时，1.将厂家对应的名称赋值给厂家名称 2.将厂家的id(即银行卡号，赋值给银行卡号)
    form.on('select(btyyBxdaId)',function (data) {
        var btyyBxdaIdView= $("#btyyBxdaId").find("option:selected").text();
        $("#btyyBxdaIdView").val("");
        if(btyyBxdaIdView!="请选择") {
            $("#btyyBxdaIdView").val(btyyBxdaIdView);
        }
        var btyyBxdaId= $("#btyyBxdaId").val();
        $("#yhKahao").val("");
        if(btyyBxdaId!=""){
            $("#yhKahao").val(btyyBxdaId);
        }

    });
    //选择货品时，将货品名称赋值给摘要
    form.on('select(btyZxHuopinId)',function (data) {
        var btyZxHuopinIdView= $("#btyZxHuopinId").find("option:selected").text();
        $("#btyZxHuopinIdView").val("");
        if(btyZxHuopinIdView!="请选择") {
            $("#zhaiyao").val(btyZxHuopinIdView);
        }
    });


});